package com.framework.modules.job.task;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.framework.entity.flow.FlowAccount;
import com.framework.entity.push.PushConfig;
import com.framework.entity.push.PushContent;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.sys.SysDept;
import com.framework.entity.sys.SysLvRate;
import com.framework.entity.ter.TerMerchantReport;
import com.framework.entity.ter.TerTerminalInfo;
import com.framework.entity.tran.TranBenefitDay;
import com.framework.entity.tran.TranTerBenefitDay;
import com.framework.entity.tran.TranTerBenefitInfo;
import com.framework.entity.tran.TranTransactionInfo;
import com.framework.service.*;
import com.framework.utils.DateUtils;
import com.framework.utils.push.JiGuangPush;
import com.framework.utils.string.StringUtil;
import com.framework.view.PidRateView;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 根据报备机具来计算分润
 *
 * @author wq
 */
@Transactional(rollbackFor = Exception.class)
@Component("tranBenefitByMer")
public class TranBenefitByMer {

    @Autowired
    private TransactionInfoService transactionInfoService;
    @Autowired
    protected TranTerBenefitDayService tranTerBenefitDayService;
    @Autowired
    private BenefitDayService benefitDayService;
    @Autowired
    private FlowAccountService flowAccountService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private PushConfigService pushConfigService;

    @Autowired
    private TerFlowInfoService terFlowInfoService;


    private Logger logger = LoggerFactory.getLogger(getClass());

    private Date benefitDate;
    private String benefitDateStr;
    private Date today;

    public void countBenefit() {
        //初始化时间
        initialDate();
        //计算分润
        countPersonalBenefit();
        //添加流水信息
        addFlowAccount();
        //推送通知
        pushBenefitNotice();
        //添加信息
        addSnInfo();
    }

    private void initialDate() {
        //获取当前日期
        today = DateUtils.stringToDate(DateUtils.format(new Date()), DateUtils.DATE_PATTERN);
        benefitDate = DateUtils.addDateDays(today, -1);
        benefitDateStr = DateUtils.format(benefitDate, DateUtils.DATE_PATTERN);
    }

    /**
     * 计算每个报备机器的交易分润,具体到机器
     */
    private void countPersonalBenefit() {

        logger.info("开始计算分润");
        List<SysDept> deptList = deptService.getDeptListExcept(889326L, 889325L);

        for (SysDept sysDept : deptList) {
            transactionInfoService.benefitByMerchant(sysDept,benefitDateStr);
        }
        logger.info("分润计算完毕");
    }

    /**
     * 导入流水
     */
    public void addFlowAccount() {

        logger.info("流水记录导入开始");
        //查询今天的分润汇总
        List<TranBenefitDay> list = benefitDayService.queryToAddFlowAccount(benefitDateStr);

        List<TranBenefitDay> tranBenefitDays = list.stream()
                .filter(e -> !e.getDeptId().equals(889325L))
                .filter(e -> !e.getDeptId().equals(889326L))
                .collect(Collectors.toList());

        for (TranBenefitDay tranBenefitDay : tranBenefitDays) {

            flowAccountService.addFlowAccountAfterBenefit(tranBenefitDay);

        }
        logger.info("流水记录导入结束");
    }

    /**
     * 推送分润通知
     */
    private void pushBenefitNotice() {
        List<SysDept> list = deptService.getDeptListExcept(889326L, 889325L);
        for (SysDept sysDept : list) {

            logger.info("推送消息开始");
            //查询今天的分润汇总
            List<TranBenefitDay> benefitDays = benefitDayService.queryPushFlowAccount(benefitDateStr, sysDept.getDeptId());

            List<String> resultList = benefitDays.stream().
                    filter(e -> e.getAllAmount().compareTo(new BigDecimal("0.00")) == 1)
                    .map(e -> e.getAgentId().toString())
                    .collect(Collectors.toList());

            List<String[]> aliasList = new ArrayList<>();

            String[] agentStrs = resultList.toArray(new String[resultList.size()]);

            aliasList.add(agentStrs);

            PushConfig pushconfig = pushConfigService.queryPushConfigByDeptId(sysDept.getDeptId());
            if (pushconfig != null) {

                for (String[] alias : aliasList) {
                    PushContent pushContent = new PushContent();
                    pushContent.setPushType("alias");
                    pushContent.setTitle("每日分润");
                    pushContent.setAlert("您的分润已到账,点击查看");
                    pushContent.setNoticeType("ass");
                    pushContent.setAssType(1);
                    pushContent.setAlias(alias);
                    JiGuangPush.jiguangPush(pushContent, pushconfig);
                }


            }
        }
        logger.info("推送消息结束");
    }


    private void addSnInfo() {

        List<TranTransactionInfo> list = transactionInfoService.selectList(new EntityWrapper<TranTransactionInfo>()
                .eq("transaction_date", benefitDateStr)
                .eq("is_benefited", 0));
        for (TranTransactionInfo info : list) {
            terFlowInfoService.addVerifyTranInfo(info.getSn(), info.getDeptId());
        }

    }


}
